package com.ado.bazinga.core.assemble;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.ado.bazinga.core.common.params.vo.GoodVO;
import com.ado.bazinga.core.common.params.vo.OrderVO;
import com.ado.bazinga.core.common.params.vo.UserParam;
import com.ado.bazinga.core.dal.entity.Good;
import com.ado.bazinga.core.dal.entity.Order;
import com.ado.bazinga.core.dal.entity.User;
import com.ado.bazinga.core.service.GoodService;
import com.ado.bazinga.core.service.OrderService;
import com.ado.bazinga.core.service.UserService;

/**
 *@author adonais
 *@date 2020年4月2日
 *@description OrderAssemble.java
 **/
@Component(value = "orderAssemble")
public class OrderAssemble {
	
	@Autowired
	@Qualifier(value = "orderService")
	private  OrderService orderService ;
	@Autowired
	@Qualifier(value = "userService")
	private  UserService userService ;
	@Autowired
	@Qualifier(value = "goodService")
	private  GoodService goodService ;

	/***
	 * 
	 * @param order
	 * @return
	 */
	public  OrderVO assembleEntityToVO(Order order) {
		OrderVO orderVO = new OrderVO();
		orderVO.setAmount(order.getAmount().toString());
		orderVO.setId(order.getId().toString());
		orderVO.setOrderNo(order.getOrderNo());
		if (!StringUtils.isEmpty(order.getGoodId().toString())) {
			Good good = goodService.getGoodById(order.getGoodId()) ;
			GoodVO goodVo = new GoodVO() ;
			goodVo.setId(good.getId().toString());
			goodVo.setIntroduction(good.getIntroduction());
			goodVo.setName(good.getName());
			goodVo.setOriginalPrice(good.getOriginalPrice().toString());
			goodVo.setPic(good.getPic());
			goodVo.setPresentPrice(good.getPresentPrice().toString());
			goodVo.setSold(good.getSold().toString());
			goodVo.setStock(good.getStock().toString());
			goodVo.setType(good.getType());
			orderVO.setGood(goodVo);
		}
		if (!StringUtils.isEmpty(order.getUserId())) {
			UserParam userParam = new UserParam();
			User user = userService.findUserById(order.getUserId()) ;
			userParam.setId(user.getId().toString());
			userParam.setUsername(user.getUsername());
			orderVO.setUser(userParam);
		}
		return orderVO;
	}
	
	/***
	 * 
	 * @param orderList
	 * @return
	 */
	public  List<OrderVO> assembleBatchToVO(List<Order> orderList) {
		List<OrderVO> orList = new ArrayList<OrderVO>();
		if (orderList.size() > 0) {
			for (Order order : orderList) {
				OrderVO orderVo = new OrderVO();
//				try {
//					BeanUtils.copyProperties(orderVo, order);
//				} catch (IllegalAccessException | InvocationTargetException e) {
//					e.printStackTrace();
//				}
				orderVo.setAmount(order.getAmount().toString());
				orderVo.setId(order.getId().toString());
				orderVo.setOrderNo(order.getOrderNo());
				if (!StringUtils.isEmpty(order.getGoodId().toString())) {
					Good good = goodService.getGoodById(order.getGoodId()) ;
					GoodVO goodVo = new GoodVO() ;
					goodVo.setId(good.getId().toString());
					goodVo.setIntroduction(good.getIntroduction());
					goodVo.setName(good.getName());
					goodVo.setOriginalPrice(good.getOriginalPrice().toString());
					goodVo.setPic(good.getPic());
					goodVo.setPresentPrice(good.getPresentPrice().toString());
					goodVo.setSold(good.getSold().toString());
					goodVo.setStock(good.getStock().toString());
					goodVo.setType(good.getType());
					orderVo.setGood(goodVo);
				}
				if (!StringUtils.isEmpty(order.getUserId())) {
					UserParam userParam = new UserParam();
					User user = userService.findUserById(order.getUserId()) ;
					userParam.setId(user.getId().toString());
					userParam.setUsername(user.getUsername());
					orderVo.setUser(userParam);
				}
				orList.add(orderVo);
			}
		}
		return orList;
	}
}
